package com.bt7274.service.impl;

import com.bt7274.mapper.NotificationMapper;
import com.bt7274.pojo.Notification;
import com.bt7274.service.NotificationService;
import com.bt7274.utils.SessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class NotificationServiceImpl implements NotificationService {

    @Autowired
    private NotificationMapper notificationMapper;

    @Override
    public void addNotification(Notification notification) {
        notification.setCreatedTime(LocalDateTime.now());
        notification.setIsRead(false);
        notificationMapper.insertNotification(notification);
    }

    @Override
    public List<Notification> getUserNotifications(String userId, int offset, int pageSize) {
        return notificationMapper.selectNotificationsByUserId(userId, offset, pageSize);
    }

    @Override
    public long countUnreadNotifications() {
        String userId=SessionUtils.getCurrentUserId();
        return notificationMapper.countUnreadNotifications(userId);
    }

    @Override
    public void markAsRead(String notificationId) {
        notificationMapper.updateNotificationAsRead(notificationId);
    }

    @Override
    public void markAllAsRead() {
        String userId= SessionUtils.getCurrentUserId();
        notificationMapper.updateAllNotificationsAsRead(userId);
    }
}
